const app = getApp();

var util = require('../../utils/util.js');
var gql = require('wxapp-graphql')
var GraphQL =gql.GraphQL;
var stadium = {};
var startTime = util.startTime(new Date());
var endTime = util.endTime(new Date());     
Page({
  data: {   
    /*================
    *    搜索框 变量
    *=================
    */   
   startTime: startTime,
   endTime: endTime,
    showTime: false, // 展示日期选择组件   
    indexAcility: 0,  
    indexType: 0,  
    pitch_acility: ['所有','室内','室外'] ,// 场地选择类型
    pitch_type : ['所有','5人制','7人制'],// 赛制选择类型
    /*===============
    *   球馆信息
    *===============
    */
    // 球馆信息（接口获取）    
    stadium:[ 
      { StadiumId :1 ,StadiumName: '城市天空',StadiumAcility: 1}, 
      { iStadiumId :2 ,StadiumName: '西部动力',StadiumAcility: 2},     
    ],           
    // 球场信息（接口获取）
    pitchList: [
      {pitch_id:1 ,pitch_no:1,checked: true,pitch_type: 1},
      {pitch_id:2 ,pitch_no:2,checked: true,pitch_type: 1}, 
      {pitch_id:3 ,pitch_no:3,checked: true,pitch_type: 2}, 
      {pitch_id:4 ,pitch_no:4,checked: true,pitch_type: 2}      
    ],
    //预定信息(接口获取)
    fix : [    
      {pitch_fix_id:1,pitch_id : 1,stadium_id:1,pitch_no : 1,time: '09:30--11:30',checked: true,team: '皇家马德里 vs 拜仁慕尼黑',hot: false,},
      {pitch_fix_id:2,pitch_id : 1,stadium_id:1,pitch_no : 1,time: '10:30--12:00',checked: false,hot: false,}, 
      {pitch_fix_id:3,pitch_id : 1,stadium_id:1,pitch_no : 1,time: '13:00--14:30',checked: false,hot: false,}, 
      {pitch_fix_id:4,pitch_id : 1,stadium_id:1,pitch_no : 1,time: '14:30--16:00',checked: true,team: '巴塞罗那 vs 切尔西',hot: false,}, 
      {pitch_fix_id:5,pitch_id : 1,stadium_id:1,pitch_no : 1,time: '16:00--17:30',checked: false,hot: false,}, 
      {pitch_fix_id:6,pitch_id : 1,stadium_id:1,pitch_no : 1,time: '17:30--19:00',checked: false,hot: false,}, 
      {pitch_fix_id:7,pitch_id : 1,stadium_id:1,pitch_no : 1,time: '19:00--20:30',checked: false,hot: false,}, 
      {pitch_fix_id:8,pitch_id : 1,stadium_id:1,pitch_no : 1,time: '20:30--22:00',checked: false,hot: false,}
    ],
    //预定成功信息(接口获取)
    loading: false,
    fix_success : [
      {pitch_fix_id: 9,pitch_id : 1,stadium_id:1,pitch_no : 1,time: '09:30--11:30'}
    ],
    //地图坐标
    latitude: 34.193332,
    longitude:108.93692,
    markers: [{
      id: 1,
      latitude: 34.193332,
      longitude:108.93692,
      name: '西安华普足球俱乐部(城市立方)'
    }],
    covers: [{
      latitude: 34.193332,
    longitude:108.93692,
      iconPath: '/image/location.png'
    }, {
      latitude: 34.193332,
    longitude:108.93692,
      iconPath: '/image/location.png'
    }]
  },        
   // 球馆信息
   queryStadium : function(){      
    let gql = GraphQL({
      url : 'http://192.168.0.177:3000/stadium'
    },true)
    gql.query({query: `
    query {
      queryStadium{
        StadiumId
        StadiumName        
        StadiumAcility
      }
    }`
    }).then(function(res){              
      stadium =JSON.stringify(res)          
    }).catch(function(error){
      console.log(error)
    })   
  },
  /**
   * =============================
   *           日期选择器
   * =============================
   */
  submit: function(e) {
    this.setData({      
      startTime: e.detail.dateStr
    })
  },
  showDatePickerPlus: function() {
    this.setData({
      showTime: true
    })
  },
  /**
   * =============================
   *           场地&赛制选择器
   * =============================
   */  
  acilityChange(e) {     
    this.setData({
      indexAcility: e.detail.value,      
    })
  },
  typeChange(e) {  
    this.setData({
      indexType: e.detail.value
    })
  },
  /**=============================
   *    球场展示
   * =============================
   */  
  showModal(e) {
    this.setData({
      modalName: e.currentTarget.dataset.target
    })
  },
  hideModal(e) {
    this.setData({
      modalName: null
    })
  }, 
  /***
   * ========================================
   *               时间预定模态组件
   * ========================================
   * */
  //展开模态组件
  showModal(e) {
    this.setData({
      modalName: e.currentTarget.dataset.target
    })
  },
  //点击取消关闭模态组件
  hideModal(e) {
    this.setData({
      modalName: null
    })
  },
  //点击确认关闭模态组件
  confirmHideModal(e) {
    e.currentTarget.dataset  
    this.setData({
      modalName: null,
      loading: true
    })
  },
  // 点击时间触发事件
  ChooseCheckbox(e) {
    let items = this.data.fix;
    let no = this.data.pitchNo;
    let time = this.data.time2Time;
    let pitch_fix_ids = e.currentTarget.dataset.value;        
    for (let i = 0, lenI = items.length; i < lenI; ++i) {            
      if (items[i].pitch_fix_id == pitch_fix_ids) {        
        if(items[i].checked){
          items[i].checked = false
        }else{
          items[i].checked = true          
          items[i].team = '利物浦 vs AC米兰'
          no = items[i].pitch_no + '号球场'
          time = items[i].time
        }                
      }
    }
    this.setData({
      fix: items,
      pitchNo : no,   
      time2Time : time   
    })
  },
})

